@extends('layouts._app')
@section('content')
    <style>
        .sure {
            display: inline-block;
            height: 32px;
            width: 32px;
            text-align: center;
            line-height: 30px;
            margin-right: 5px;
            font-size: 15px;
            border-right: 1px solid #FFF;
            background: url(/static/admin/icon/sure.png) no-repeat center;
            background-size: 20px;
        }

    </style>
    <body class="iframe-h">
    <div class="wrap-container email-wrap clearfix">
        <div class="row">
            <div class="email-edge col-lg-2">
                <a href="{{route('expansion.email.create')}}" class="layui-btn layui-btn-normal email-write-btn">写信</a>
                <dl class="email-folder">
                    <dt>文件夹</dt>
                    <dd>
                        <a href="{{route('expansion.email.index')}}?title=EmailReceive"><i class="iconfont">&#xe639;</i>收件箱
                            {!! Form::submit("{$data['receive']}",['class'=>'layui-btn  layui-btn-danger layui-btn-mini f-r']) !!}
                        </a>
                    </dd>
                    <dd>
                        <a href="{{route('expansion.email.index')}}?title=EmailSend"><i class="iconfont">&#xe603;</i>发信
                            {!! Form::submit("{$data['sends']}",['class'=>'layui-btn  layui-btn-danger layui-btn-mini f-r']) !!}
                        </a>
                    </dd>
                </dl>
            </div>
            <div class="col-lg-10">
                <div class="email-content" style="height: inherit;">
                    <div class="email-content-title layui-form">
                    <span
                        class="write-title">@if($request['title'] == 'EmailSend') 发信 ( @else
                            收件箱 ( @endif {{$email->total()}} )
                       </span>
                    </div>
                    <div class="email-tool-btn clearfix">
                        @include('layouts._flash')
                        <div class="col-lg-6 email-tool-btn-left">
                            <a href="{{route('expansion.email.index')}}?title={{$request['title']}}&name={{$request['name']}}"
                               class="layui-btn layui-btn-small layui-btn-primary" id='email-refresh-btn'
                               data-title='刷新'>
                                <i class="layui-icon">&#x1002;</i>
                            </a>
                            @if($request['title'] == 'EmailReceive')
                                <a href="javascript:void(0);"
                                   class="layui-btn layui-btn-small layui-btn-primary" id='LaZaRd'
                                   data-title='拉取'>
                                    <i class="fa fa-hand-lizard-o"></i>
                                </a>
                            @endif
                            {!! Form::open(['class'=>'layui-form','style'=>'float: right;']) !!}
                            <div class="layui-inline">
                                <select id="change_category" lay-filter="category" lay-search
                                        data-id="{{$request['table']}}">
                                    <option selected>下载</option>
                                    <option>.txt</option>
                                    <option>.xlsx</option>
                                </select>
                            </div>
                            {!! Form::close() !!}
                        </div>
                        {!! Form::open(['route' => ['expansion.email.index'],'method'=>'get']) !!}
                        {!! Form::hidden('title',"{$request['title']}") !!}
                        <div class="col-lg-6 email-tool-btn-right">
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    {!! Form::text('name',"{$request['name']}",['class'=>'layui-input','placeholder'=>'搜索邮件标题']) !!}
                                </div>
                                <div class="layui-inline">
                                    {!! Form::submit('搜索',['class'=>'layui-btn layui-btn-normal search-btn']) !!}
                                </div>
                            </div>
                        </div>
                        {!! Form::close() !!}
                    </div>
                    <div class="layui-form email-list">
                        <table class="layui-table" lay-even lay-skin="nob">
                            <thead>
                            <tr>
                                <th class="hidden-xs">名称来源</th>
                                @if($request['title'] == 'EmailSend')
                                    <th class="hidden-xs">收件地址</th>
                                @endif
                                <th><span style="margin: 0 0 0 180px">标题</span></th>
                                @if($request['title'] == 'EmailReceive')
                                    <th>状态</th>
                                @endif
                                <th>创建时间</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            @foreach($email as $value)
                                <tr>
                                    <td>
                                        <a href="{{route('expansion.email.show',$value->id)}}?title={{$request['title']}}&name={{$request['name']}}&page={{$request['page']}}"
                                           title="{{$value->name}}({{$value->sender}})">
                                            {{$value->name}}({{$value->sender}})
                                        </a>
                                    </td>
                                    @if($request['title'] == 'EmailSend')
                                        <td>
                                            <a href="{{route('expansion.email.show',$value->id)}}?title={{$request['title']}}&name={{$request['name']}}&page={{$request['page']}}">
                                                {{$value->receipt}}
                                            </a></td>
                                    @endif
                                    <td style="padding:5px 0;">
                                        <a style="display: flex;top:3px;"
                                           href="{{route('expansion.email.show',$value->id)}}?title={{$request['title']}}&name={{$request['name']}}&page={{$request['page']}}">
                                            @if($value->file)
                                                <icon class="layui-icon sure"></icon>
                                            @endif
                                            <p style="    margin: auto;" title="{{$value->title}}">
                                                {{sub($value->title,20)}}
                                            </p>
                                        </a>
                                    </td>
                                    @if($request['title'] == 'EmailReceive')
                                        <td>@if($value->status == 1) 未读 @else 已读 @endif</td>
                                    @endif
                                    <td title="{{$value->created_at}}">{{sub($value->created_at,20)}}</td>
                                    <td><a class="layui-btn layui-btn-small layui-btn-danger"
                                           href="@if($value->eid){{route('expansion.email.destroy',$value->eid)}}@else{{route('expansion.email.destroy',$value->id)}}@endif?title={{$request['title']}}&id={{$value->id}}"
                                           data-method="delete" title="删除"
                                           data-token="{{csrf_token()}}" data-confirm="确认删除吗?">
                                            <i class="layui-icon">&#xe640;</i></a></td>
                                </tr>
                            @endforeach
                            </tbody>
                        </table>
                        <div class="page-wrap">
                            <p style="float: left;">注 : 共 {{$email->total()}} 条记录</p>
                            {{ $email->links() }}
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="{{asset('static/admin/layui/layui.js')}}" type="text/javascript" charset="utf-8"></script>
    <script src="{{asset('static/admin/js/common.js')}}" type="text/javascript" charset="utf-8"></script>
    <script src="{{asset('js/jquery.min.js')}}" type="text/javascript" charset="utf-8"></script>
    <script src="{{asset('js/destroy.js')}}" type="text/javascript" charset="utf-8"></script>
    <script>
        layui.use(['jquery', 'dialog', 'form'], function () {
            var $ = layui.jquery;
            var dialog = layui.dialog;
            var form = layui.form();
            form.render();
            //顶部批量删除
            $('#LaZaRd').click(function () {
                dialog.confirm({
                    message: '您确定要拉取？这可能会耗费几分钟时间',
                    success: function () {
                        $.ajax({
                            type: 'GET',
                            url: '{{route('expansion.email.laZaRd')}}',
                            dataType: "json",
                            beforeSend: function () {
                                layer.load(1, {
                                    shade: [0.1, '#333333'] //0.1透明度的白色背景
                                });
                            },
                            complete: function (XHR) {
                                XHR = null;
                            },
                            error: function (xhr) {//获取ajax的错误信息
                                layer.alert(xhr.responseText, "Failed", function () {
                                    layer.closeAll();
                                });
                            },
                            success: function (res) {
                                layer.closeAll();
                                if (res.code != 200) {
                                    layer.msg(res.msg, {icon: 5, time: 3000})
                                    return false;
                                }
                                if (res.code == 403) {
                                    layer.msg(res.msg, {icon: 4, time: 3000})
                                    return false;
                                }
                                layer.msg(res.msg, {icon: 1, time: 3000}, function () {
                                    window.location.reload();
                                });
                            }
                        })
                    },
                    cancel: function () {
                        return false;
                    }
                })
                return false;

            }).mouseenter(function () {

                dialog.tips('拉取邮件', '#LaZaRd');

            })


            form.on('select(category)', function (e) {
                if (e.value == '下载') {
                    return false;
                }
                /**
                 * 写入文件下载
                 * @param id
                 */
                var table = $("#change_category").data('id');
                var ext = e.value;
                dialog.confirm({
                    message: '这可能会耗费很长时间！是否继续',
                    success: function () {
                        window.open('{{route('TextFile')}}' + '?table=' + table + '&ext=' + ext, '_parent');
                        window.location.reload();
                    },
                });
            });

            form.render();


        })
    </script>
    </body>
@endsection
